//
//  Client_RF225_qArticleController.swift
//  Wmsmobile
//
//  Created by xuNick on 16/9/11.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class Client_RF225_qArtInfoController: HDFounctionBaseController , HDBarcodeInputCellInputDelegate {
//    //MARK: - property
//    var dataArray:[[String]] = [
//        ["货品条码" , "货品代码", "货品名称"],
//        [""],
//        [""],
//        ["供应商代码" , "供应商名称" , "退货位"]
//    ]
    var dataArray0:[String] = ["货品条码" , "货品代码", "货品名称"]
    var dataArray1:[[String : String]]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    var dataArray2:[[String : String]]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    var dataArray3:[[String : String]] = [["title" : "供应商代码"] , ["title" : "供应商名称"] , ["title" : "退货位"]]

    //MARK: - lifeCycle
    override func viewDidLoad() {
        self.tableView.register(HDBarcodeInputCell.self, forCellReuseIdentifier: "HDBarcodeInputCell")
        self.tableView.register(HDProductInfocell.self, forCellReuseIdentifier: "HDProductInfocell")
        self.tableView.register(HDKeyboardInputCell.self, forCellReuseIdentifier: "HDKeyboardInputCell")
        self.tableView.register(HDChkBinInfoTableviewPickCell.self, forCellReuseIdentifier: "HDChkBinInfoTableviewPickCell")
        self.tableView.register(HDChkBinInfoTableviewCell.self, forCellReuseIdentifier: "HDChkBinInfoTableviewCell")
        super.viewDidLoad()
    }
    override func viewDidAppear(_ animated: Bool) {
        let cell = self.tableView.cellForRow(at: IndexPath(item: 0, section: 0)) as! HDBarcodeInputCell
        cell.state = inputViewState.activated
    }
    //MARK: - delegate

    func clicKReturn(_ cell: HDBarcodeInputCell) {
        cell.state = inputViewState.disabled
        let celltext = cell.codeInputView.textField.text
        if celltext?.characters.count == 0 {
            self.showMessage("请输入货品条码", success: false)
            cell.state = inputViewState.wrong
            return
        }
        self.oneToManyHandler(celltext!, piOwner: "-1", piBin: nil, success: { (dict) in
            self.getData(["piArticle" : dict["内码"]!], bodyMethod: "qryArticleInfo", suffix: nil, success: { (dict) in
                cell.codeInputView.textField.text = nil
                let originString = dict["poArtMsg"]
                            let cell1 = self.tableView.cellForRow(at: IndexPath(row: 1, section: 0)) as! HDProductInfocell
                            let cell2 = self.tableView.cellForRow(at: IndexPath(row: 2, section: 0)) as! HDProductInfocell
                var tempArray = [[String : String]]()
                guard strlen(originString ?? "") > 0 else {
                    cell.state = inputViewState.wrong
                    return
                }
                let string1 = originString?.components(separatedBy: ";; ;;")[2]
                var stringArray1 = string1?.components(separatedBy: ";;")
                let string2 = originString?.components(separatedBy: ";; ;;")[0]
                var stringArray2 = string2?.components(separatedBy: ";;")
                cell1.textField.text = stringArray2![0].components(separatedBy: ":").last!
                cell2.textField.text = stringArray2![1].components(separatedBy: ":").last!
                print(cell2.textField.text!)
                let string3 = originString?.components(separatedBy: ";; ;;")[3]
                var stringArray = string3?.components(separatedBy: ";;")
                for i in 0...2 {
//                    let proCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: 3)) as! HDProductInfocell
//                    let string1 = originString?.componentsSeparatedByString(";; ;;")[3]
//                    var stringArray = string1?.componentsSeparatedByString(";;")
//
//                    switch i {
//                    case 0 :
//                        proCell.textField.text = stringArray![0].componentsSeparatedByString(":").last
//                        break
//                    case 1 :
//                        proCell.textField.text = stringArray![1].componentsSeparatedByString(":").last
//                        break
//                    case 2:
//                        proCell.textField.text = stringArray![2].componentsSeparatedByString(":").last
//                        break
//                    default:
//                        break
//                    }
                    self.dataArray3[i].updateValue(stringArray![i].components(separatedBy: ":").last ?? "", forKey: "text")
                    print(self.dataArray3[i])
                   
                }
                stringArray1?.removeFirst()
                for item1 in stringArray1! {
                    var dict = [String : String]()
                    let array = item1.components(separatedBy: " ")
                    let value1 = array[0].components(separatedBy: ":").last
                    dict.updateValue(value1!, forKey: "拣货规格")
                    let value2 = array[1].components(separatedBy: ":").last
                    dict.updateValue(value2!, forKey: "拣货位")
                    tempArray.append(dict)
                }
                self.dataArray2 = tempArray
                
                var tempArrayTop = [[String : String]]()
                guard strlen(originString ?? "") > 0 else {
                    cell.state = inputViewState.wrong
                    return
                }
                let string1Top = originString?.components(separatedBy: ";; ;;")[1]
                var stringArray1Top = string1Top?.components(separatedBy: ";;")
                stringArray1Top?.removeFirst()
                for item1 in stringArray1Top! { //收货规格:1*1 长:10 宽:6 高:3 重:400
                    var dict = [String : String]()
                    let array = item1.components(separatedBy: " ")
                    print("___------\(array)")
                    let value1 = array[0].components(separatedBy: ":").last
                    print("<<<<<<<\(value1)")
                    dict.updateValue(value1!, forKey: "left")
                    let value2 = "\(array[1].components(separatedBy: ":").last ?? "")/\(array[2].components(separatedBy: ":").last ?? "")/\(array[3].components(separatedBy: ":").last ?? "")"
                    dict.updateValue(value2, forKey: "middle")
                    let value3 = array[4].components(separatedBy: ":").last
                    dict.updateValue(value3!, forKey: "right")
                    tempArrayTop.append(dict)
                }
                self.dataArray1 = tempArrayTop
                cell.state = inputViewState.activated
                }, failed: {_ in 
                  cell.state = inputViewState.wrong
            })
            }) {
               cell.state = inputViewState.wrong 
        }
    }
    //MARK: - private
    //MARK: - tableviewDatasource
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 3
        case 1:
            return dataArray1?.count ?? 1
        case 2:
            return dataArray2?.count ?? 1
        case 3:
            return 3
        default:
            return 0
        }
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellOriginal:HDFounctionBaseCell?
        switch ((indexPath as NSIndexPath).section) * 100 + ((indexPath as NSIndexPath).row) {
        case 0:
            cellOriginal = tableView.dequeueReusableCell(withIdentifier: "HDBarcodeInputCell") as! HDBarcodeInputCell
            let cell = cellOriginal as! HDBarcodeInputCell
            cell.InputDelegate = self
        case 1...2 , 300...302:
            cellOriginal = tableView.dequeueReusableCell(withIdentifier: "HDProductInfocell") as! HDProductInfocell
        case 100...109:
            cellOriginal = tableView.dequeueReusableCell(withIdentifier: "HDChkBinInfoTableviewCell") as! HDChkBinInfoTableviewCell
            let cell = cellOriginal as! HDChkBinInfoTableviewCell
            if let dataArr = dataArray1{
                cell.dict = dataArr[(indexPath as NSIndexPath).row]
            }else{
                cell.dict = ["middle" : "无数据"]
            }
        case 200...209:
            cellOriginal = tableView.dequeueReusableCell(withIdentifier: "HDChkBinInfoTableviewPickCell") as! HDChkBinInfoTableviewPickCell
            let cell = cellOriginal as! HDChkBinInfoTableviewPickCell
            if let dataArr = dataArray2{
                cell.dict = dataArr[(indexPath as NSIndexPath).row]
            }else{
                cell.dict = ["无数据" : "无数据"]
            }

        default:
            cellOriginal = tableView.dequeueReusableCell(withIdentifier: "HDProductInfocell") as! HDProductInfocell
        }
        if !self.reuseFlag {
            cellOriginal?.clearContent()
        }
        switch (indexPath as NSIndexPath).section {
        case 0:
           cellOriginal!.titleView.text = dataArray0[(indexPath as NSIndexPath).row]
        case 3:
            cellOriginal!.info = dataArray3[(indexPath as NSIndexPath).row]
        default:
            break
        }
        
        return cellOriginal!
    }

    //MARK: - tableViewDelegate
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        //           let sectionHeaders:[String] = [,]
        switch section {
        case 1:
            let view = FounctionSectionHeaderView(headerString: "收货规格,长/宽/高,重量")
            return view
        case 2:
            let view = FounctionSectionHeaderView(headerString: "拣货位,拣货规格")
            return view
        default:
            return nil
        }
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return 10
        case 1,2:
            return 40
        default:
            return 0
        }
    }
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
}
